package net.warl0ck.mjjp;

import java.util.Vector;
import net.warl0ck.mjjp.MJTypes.*;

public class MJFile {
	private Vector<MJName> names = new Vector<MJName>();
	private Vector<MJHand> hands = new Vector<MJHand>();
	private Vector<MJHand> opens = new Vector<MJHand>();
	private Vector<MJHand> flower = new Vector<MJHand>();
	private Vector<MJDiscard> disc = new Vector<MJDiscard>();
	private Vector<MJLog> log = new Vector<MJLog>();
	private Vector<String> info = new Vector<String>();
	private String locale;
	public MJFile(MJParams params){
		names.clear();
		initWind("E", "Player1",params);
		initWind("S", "Player2",params);
		initWind("W", "Player3",params);
		initWind("N", "Player4",params);
		setLoc(params.getLoc());
	}
	public void initWind(String wind,String name, MJParams params){
		addName(wind, name);
		setHand(new MJHand(wind,new Vector<MJTail>(),getLoc()));
		getHandByWind(wind).setMaxtiles(params.getMaxTail());
		setOpen(new MJHand(wind,new Vector<MJTail>(),getLoc()));
		setFlower(new MJHand(wind,new Vector<MJTail>(),getLoc()));
		setDiscard(new MJDiscard(wind,new Vector<MJTail>()));
	}
	public void addName(String wind, String name){
		boolean exist = false;
		for (MJName n: names)
			if (n.getWind().equalsIgnoreCase(wind)){
				exist = true;
				n.setName(name);
			}
		if (! exist)
			names.add(new MJName(wind, name));
	}
	public void setHand(MJHand hand){
		boolean exist = false;
		for (MJHand h: hands)
			if (h.getWind().equalsIgnoreCase(hand.getWind())){
				exist = true;
				h.setHand(hand.getHand());
			}
		if (! exist)
			hands.add(new MJHand(hand.getWind(), hand.getHand(),getLoc()));
	}
	public void setOpen(MJHand open){
		boolean exist = false;
		for (MJHand h: opens)
			if (h.getWind().equalsIgnoreCase(open.getWind())){
				exist = true;
				h.setHand(open.getHand());
			}
		if (! exist)
			opens.add(new MJHand(open.getWind(), open.getHand(),getLoc()));
	}
	public void setDiscard(MJDiscard dis){
		boolean exist = false;
		for (MJDiscard d: disc)
			if (d.getWind().equalsIgnoreCase(dis.getWind())){
				exist = true;
				d.setDiscard(dis.getDiscard());
			}
		if (! exist)
			disc.add(new MJDiscard(dis.getWind(), dis.getDiscard()));
	}
	public void setFlower(MJHand flwr){
		boolean exist = false;
		for (MJHand h: flower)
			if (h.getWind().equalsIgnoreCase(flwr.getWind())){
				exist = true;
				h.setHand(flwr.getHand());
			}
		if (! exist)
			flower.add(new MJHand(flwr.getWind(), flwr.getHand(),getLoc()));
	}
	public Vector<MJName> getNames(){
		return names;
	}
	public String getNameByWind(String wind){
		for (MJName n: names)
			if (n.getWind().equalsIgnoreCase(wind)) return n.getName();
		return null;
	}
	public Vector<MJHand> getHands(){
		return hands;
	}
	public Vector<MJHand> getOpens(){
		return opens;
	}
	public Vector<MJDiscard> getDiscards(){
		return disc;
	}
	public MJHand getHandByWind(String wind){
		for (MJHand h: hands)
			if (h.getWind().equalsIgnoreCase(wind)) return h;
		return null;
	}
	public MJHand getOpenByWind(String wind){
		for (MJHand o: opens)
			if (o.getWind().equalsIgnoreCase(wind)) return o;
		return null;
	}
	public MJDiscard getDiscardByWind(String wind){
		for (MJDiscard d: disc)
			if (d.getWind().equalsIgnoreCase(wind)) return d;
		return null;
	}
	public void setLog(Vector<MJLog> log) {
		this.log = log;
	}
	public void addLog(MJLog log) {
		this.log.add(log);
	}
	public Vector<MJLog> getLog() {
		return log;
	}
	public Vector<MJHand> getFlower() {
		return flower;
	}
	public MJHand getFlowerByWind(String wind){
		for (MJHand f: flower)
			if (f.getWind().equalsIgnoreCase(wind)) return f;
		return null;
	}
	public MJLog getLogById(int id) {
		return log.get(id-1);
	}
	public void setLoc(String locale) {
		this.locale = locale;
	}
	public String getLoc() {
		return locale;
	}
	public void setInfo(Vector<String> info) {
		this.info = info;
	}
	public Vector<String> getInfo() {
		return info;
	}
}

